home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 182_01 / undos.c < prev    next >
Text File  |  1990-07-30  |  4KB  |  181 lines

  1. /*% cc -O -K % -o undos
  2.  *
  3.  * Undos - change DOS format files to Unix, etc.
  4.  */
  5. char ID[] =
  6.  "Undos Rev 4-23-85 (C)Copyright Omen Technology Inc All Rights Reserved\n";
  7. /*
  8.  * This program and documentation may be copied, used, or modified
  9.  *  by Professional-YAM lincesees provided these notices are not removed.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15.  
  16. #define LL 1024
  17. #define SUB 032
  18.  
  19. char Lbuf[LL];
  20. char *Progname;
  21. int Todos = 0;
  22. int Tocpm = 0;
  23. int Tomac = 0;
  24. int Unmac = 0;
  25. int Strip = 0;
  26.  
  27. main(argc, argv)
  28. char **argv;
  29. {
  30.     Progname = *argv;
  31.     if (! strcmp(Progname, "tocpm"))
  32.         Todos = Tocpm = 1;
  33.     if (! strcmp(Progname, "todos"))
  34.         Todos = 1;
  35.     if (! strcmp(Progname, "unmac"))
  36.         Unmac = 1;
  37.     if (! strcmp(Progname, "tomac"))
  38.         Tomac = 1;
  39.  
  40.     if (! strcmp(argv[1], "-s")) {
  41.         ++Strip; --argc; ++argv;
  42.     }
  43.  
  44.  
  45.     if (argc<2 || *argv[1]== '-')
  46.         usage();
  47.     while (--argc >= 1)
  48.         chngfmt(*++argv);
  49.     exit(0);
  50. }
  51. usage()
  52. {
  53.     fprintf(stderr, ID);
  54.     fprintf(stderr, "Usage: {undos|tounix|todos|tocpm|unmac} [-s] file ...\n");
  55.     fprintf(stderr, "    -s Strip parity bit, ignore bytes < 007\n");
  56.     exit(1);
  57. }
  58.  
  59.  
  60. chngfmt(name)
  61. char *name;
  62. {
  63.     register c;
  64.     register char *p;
  65.     register n;
  66.     register long fpos;
  67.     struct stat st;
  68.     FILE *fin, *fout;
  69.     int linno = 0;
  70.     long ftell();
  71.     char *mktemp();
  72.     char outnam[64];
  73.  
  74.     if (stat(name, &st)) {
  75.         xperror(name); return;
  76.     }
  77.     if ((st.st_mode & S_IFMT) != S_IFREG) {
  78.         fprintf(stderr, "%s: %s is not a regular file\n", Progname, name);
  79.         return;
  80.     }
  81.     if ((fin = fopen(name, "r")) == NULL) {
  82.         xperror(name); return;
  83.     }
  84.     strcpy(outnam, "undosXXXXXX");
  85.     mktemp(outnam);
  86.     if ((fout = fopen(outnam, "w")) == NULL) {
  87.         xperror(outnam); exit(1);
  88.     }
  89.  
  90.     for (;;) {
  91.         ++linno;
  92.         for (p=Lbuf, n=LL; --n>0; ) {
  93. ignore:
  94.             if ((c = getc(fin)) == EOF)
  95.                 break;
  96.             if ( !c)
  97.                 goto ignore;
  98.             if (c < '\7' || (c & 0200)) {
  99.                 if (Strip) {
  100.                     if ((c &= 0177) < 7)
  101.                         goto ignore;
  102.                 } else
  103.                     goto thisbin; 
  104.             }
  105.             if (c == SUB)
  106.                 break;
  107.             if (c == '\r' && Unmac)
  108.                 c = '\n';
  109.             *p++ = c;
  110.             if (c == '\n')
  111.                 break;
  112.         }
  113.         *p = '\0';
  114.  
  115.         if (n == 0) {
  116.     thisbin:
  117.             if (n) {
  118.                 fprintf(stderr, "%s: %s is a binary file", Progname, name);
  119.                 fprintf(stderr, " line=%d char =%2X\n", linno, c);
  120.             } else
  121.                 fprintf(stderr, "%s: %s has no linefeeds: try unmac?\n", Progname, name);
  122.             fclose(fout);
  123.             unlink(outnam);
  124.             return;
  125.         }
  126.  
  127.         if (Todos) {
  128.             if (*--p == '\n' && p[-1] != '\r') {
  129.                 *p++ = '\r'; *p++ = '\n'; *p = 0;
  130.             }
  131.         } else if (Tomac) {
  132.             if (*--p == '\n') {
  133.                 if (p[-1] == '\r')
  134.                     --p;
  135.                 *p++ = '\r'; *p = 0;
  136.             }
  137.         } else {
  138.             if (*--p == '\n' && *--p == '\r') {
  139.                 *p++ = '\n'; *p = 0;
  140.             }
  141.         }
  142.         if (fputs(Lbuf, fout) == EOF) {
  143.             xperror(outnam); exit(1);
  144.         }
  145.         switch (c) {
  146.         case EOF:
  147.             if (ferror(fin)) {
  148.                 xperror(name); exit(0200);
  149.             }
  150.         case SUB:
  151.             if (Tocpm) {
  152.                 fpos = ftell(fout);
  153.                 do {
  154.                     putc(SUB, fout);
  155.                 } while (++fpos & 127);
  156.             }
  157.             fclose(fout); fclose(fin);
  158.             sprintf(Lbuf, "mv %s %s", outnam, name);
  159.             system(Lbuf);
  160.             utime(name, (struct utimbuf *) &st.st_atime);
  161.             return;
  162.         }
  163.     }
  164. }
  165.  
  166. xperror(s)
  167. char *s;
  168. {
  169.     register char *p;
  170.     extern int sys_nerr;
  171.     extern char *sys_errlist[];
  172.     extern errno;
  173.  
  174.     if (errno >= sys_nerr)
  175.         p = "Gloryovsky: a New Error!";
  176.     else
  177.         p = sys_errlist[errno];
  178.     fprintf(stderr, "%s: %s: %s\n", Progname, s, p);
  179. }
  180.  
  181.